function json2url(json) {
	var arr = [];
	for (var name in json) {
		arr.push(name + '=' + json[name]);
		
	}
	return arr.join('&');
}

function ajax(json) {
	json = json || {};
	if (!json.url) return;
	json.data = json.data || {};
	json.type = json.type || 'get';

	var timer = null;

	if (window.XMLHttpRequest) {
		var oAjax = new XMLHttpRequest();
	} else {
		var oAjax = new ActiveXObject('Microsoft.XMLHTTP');
	}
	switch (json.type) {
		case 'get':
			/*console.log(json.url + '?' + json2url(json.data),"json.url + '?' + json2url(json.data)")*/
			oAjax.open('GET', json.url + '?' + json2url(json.data), true);
			oAjax.send();
			break;
		case 'post':
			oAjax.open('POST', json.url, true);
			oAjax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			/*oAjax.setRequestHeader('Access-Control-Allow-Origin', '*');
			oAjax.setRequestHeader('Access-Control-Allow-Methods', 'post');
			oAjax.setRequestHeader('Access-Control-Allow-Headers', 'content-type');*/
			/*console.log(json2url(json.data),"json2url(json.data)")*/
			oAjax.send(json2url(json.data));
			break;
	}
	
	
	oAjax.onreadystatechange = function() {
		
		if (oAjax.readyState == 4) {
			clearTimeout(timer);
			if (oAjax.status >= 200 && oAjax.status < 300 || oAjax.status == 304) {
				json.success && json.success(oAjax.responseText);
			} else {
				json.error && json.error(oAjax.status);
			}
		}
	};
}

function ajax2(json) {
	json = json || {};
	if (!json.url) return;
	json.data = json.data || {};
	json.type = json.type || 'get';

	var timer = null;

	if (window.XMLHttpRequest) {
		var oAjax = new XMLHttpRequest();
		
	} else {
		var oAjax = new ActiveXObject('Microsoft.XMLHTTP');
	}
	switch (json.type) {
		case 'get':
			oAjax.open('GET', json.url + '?' + json2url(json.data), true);
			oAjax.send();
			break;
		case 'post':
			oAjax.open('POST', json.url, true);
			oAjax.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			/*oAjax.setRequestHeader('Access-Control-Allow-Origin', '*');
			oAjax.setRequestHeader('Access-Control-Allow-Methods', 'post');
			oAjax.setRequestHeader('Access-Control-Allow-Headers', 'content-type');*/
			oAjax.send(json2url(json.data));
			break;
			
	}

	oAjax.onreadystatechange = function() {
		if (oAjax.readyState == 4) {	
			clearTimeout(timer);
			if (oAjax.status >= 200 && oAjax.status < 300 || oAjax.status == 304) {
				json.success && json.success(oAjax.responseText);
			} else {
				json.error && json.error(oAjax.status);
			}
		}
	};
}

	var setFontSize = function(widthNumber) {
		var htmlSelector = document.documentElement
		var w = widthNumber
		if (widthNumber > 1600) {
			w = 1600
		}
		var fSize = w / 13.34
		htmlSelector.style.fontSize = String(fSize) + "px"
		htmlSelector.dataset.fontrem = fSize.toFixed(2)	   		
   	}

function getOffsetSum(ele){
    var top= 0,left=0;
    while(ele){
        top+=ele.offsetTop;
        left+=ele.offsetLeft;
        ele=ele.offsetParent;
    }
    return {
        top:top,
        left:left
    }
}

var getClosestParent = function(element, className) {
	var parentElement = element
	while(true) {
		if (parentElement.classList.contains(className)) {
			return parentElement
		} else {
			parentElement = parentElement.parentElement
		}
	}
}

var getRandomArray = function(length) {
	var l = length
	var randomArray = []
	for (var i = 0; i < l; i++) {
		randomArray.push(i)
	}
	
	var sortRandomArray = randomArray.sort(function() {
	     return (0.5-Math.random());
	})
	return sortRandomArray 
}

function randomFrom(lowerValue,upperValue)
{
 return Math.floor(Math.random() * (upperValue - lowerValue + 1) + lowerValue);
}

var setNoticeDivHTML = function(text) {
	   var noticeDiv = document.querySelector("#id-div-noticeDiv")
	   if(noticeDiv == null){
		   return
	   }
		noticeDiv.innerHTML = text
	   if (noticeDiv.style.display == "block" && noticeDiv.innerHTML == text) {
		   return
	   }
	   noticeDiv.style.display = "block"
	   setTimeout(function() {
		   
		   noticeDiv.style.display = ""
		   noticeDiv.innerHTML = ""
	   }, 1000)
}

var formatString2SimpleString = function(s1) {
	var index = 0
	var result = ""
	for (var i = 0; i < s1.length; i++) {
		var letter = s1[i]
		if (letter == "0") {
			index = index + 1
		}
	}
	if (index >= 4) {
		result = s1.slice(0, s1.length -4) + "万"
	}
	else if (index == 3) {
		result = s1.slice(0, s1.length - 3) + "千"
	}
	else {
		result = s1
	}
	return result
}


function GetQueryString(name) {
     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
     var r = window.location.search.substr(1).match(reg);
     if(r!=null) {
    	return  unescape(r[2]);
     }
     	return null;
}

//var getApiPath = function() {
//	//var path = "http://192.168.1.2:8080/"
//	var path = "http://103.40.10.19:8080/"
//	return path
//}

